<?php

/**
 * group actions.
 *
 * @package    pronos
 * @subpackage group
 * @author     sthiebaut
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
function cmpId($stdA, $stdB) {
	$a = $stdA->getTeamId();
	$b = $stdB->getTeamId();
    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
 function cmpDButsPour($stdA, $stdB) {
	$a = $stdA->getNbBP();
	$b = $stdB->getNbBP();
    if ($a == $b) {
        return cmpId($stdA, $stdB);
    }
    return ($a > $b) ? -1 : 1;
}
function cmpDButs($stdA, $stdB) {
	$a = $stdA->getNbBP() - $stdA->getNbBC();
	$b = $stdB->getNbBP() - $stdB->getNbBC();
    if ($a == $b) {
        return cmpDButsPour($stdA, $stdB);
    }
    return ($a > $b) ? -1 : 1;
}
function cmp($stdA, $stdB) {
	$a = $stdA->getNbPts();
	$b = $stdB->getNbPts();
    if ($a == $b) {
        return cmpDButs($stdA, $stdB);
    }
    return ($a > $b) ? -1 : 1;
}

class groupActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->pronos_groups = Doctrine::getTable('PronosGroup')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->pronos_group = Doctrine::getTable('PronosGroup')->find(array($request->getParameter('id')));
	
	$teams = $this->pronos_group->getTeams();
	
	$standings = new ArrayObject();
	
	foreach($teams as $team){
		$standing = $team->getStanding();
		$standings->Append($standing);
	}
	$standings->uasort('cmp');
	
	$this->standings = $standings;
		
    $this->forward404Unless($this->pronos_group);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PronosGroupForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PronosGroupForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($pronos_group = Doctrine::getTable('PronosGroup')->find(array($request->getParameter('id'))), sprintf('Object pronos_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new PronosGroupForm($pronos_group);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pronos_group = Doctrine::getTable('PronosGroup')->find(array($request->getParameter('id'))), sprintf('Object pronos_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new PronosGroupForm($pronos_group);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($pronos_group = Doctrine::getTable('PronosGroup')->find(array($request->getParameter('id'))), sprintf('Object pronos_group does not exist (%s).', $request->getParameter('id')));
    $pronos_group->delete();

    $this->redirect('group/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pronos_group = $form->save();

      $this->redirect('group/edit?id='.$pronos_group->getId());
    }
  }
}
